// pages/wsxx/index.js
import {
  cardInfo
} from '../../utils/api'
import {
  mobileValidate
} from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    frontID: '',
    backID: '',
    bank: '',
    err: false,
    idNum: '',
    name: '',
    validDate: '',
    bankInfo: '',
    cardNo: '',
    phone: '',
    backImage: '',
    bankcard: '',
    frontImage: '',
    disabled: false,
    info: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  choosePic(e) {
    let type = e.currentTarget.dataset.type
    let that = this
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.showLoading({
          title: '上传中',
        })
        wx.uploadFile({
          url: 'https://pay.dianbang-st.com/file/upload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'file': tempFilePaths[0]
          },
          success(res) {
            const data = JSON.parse(res.data)
            console.log(data)
            if (data.result) {
              wx.request({
                url: 'https://pay.dianbang-st.com/api/nengliang/account/doOcr?imageUrl=' + data.url + '&type=' + (type == 'bank' ? 2 : 1),
                method: 'POST',
                header: {
                  'userToken': wx.getStorageSync('userToken'),
                },
                success(res) {
                  console.log(res.data)
                  let content = res.data.content
                  wx.hideLoading()
                  if (res.data.code == 200) {
                    that.setData({
                      [type]: tempFilePaths[0],
                    })
                    if (type == 'frontID') {
                      that.setData({
                        name: content ? content.name : '',
                        idNum: content ? content.idNum : '',
                        frontImage: data.url
                      })
                    } else if (type == 'backID') {
                      that.setData({
                        validDate: content ? content.validDate : '',
                        backImage: data.url
                      })
                    } else {
                      that.setData({
                        cardNo: content ? content.cardNo : '',
                        bankInfo: content ? content.bankInfo : '',
                        bankcard: data.url
                      })
                    }
                    wx.showToast({
                      title: '操作成功',
                      icon: 'success'
                    })
                  } else {
                    wx.showToast({
                      title: res.data.desc,
                      icon: 'error'
                    })
                  }
                }
              })
            }
          }
        })
      }
    })
  },
  validMobile(e) {
    this.setData({
      err: !mobileValidate(e.detail.value)
    })
    if (!mobileValidate(e.detail.value)) {
      return
    }
    this.setData({
      phone: e.detail.value
    })
  },
  itentify() {
    const {
      idNum,
      name,
      validDate,
      bankInfo,
      cardNo,
      phone,
      backImage,
      bankcard,
      frontImage,
      disabled
    } = this.data
    if (idNum && name && validDate && bankInfo && cardNo && phone && backImage && bankcard && frontImage && !disabled) {
      this.setData({
        disabled: true
      })
      wx.request({
        url: 'https://pay.dianbang-st.com/api/nengliang/account/doRealname?backImage=' + backImage + '&bankcard=' + cardNo + '&frontImage=' + frontImage + '&idcard=' + idNum + '&name=' + name + '&phone=' + phone + '&subbranch=' + bankInfo + '&validDate=' + validDate,
        method: 'POST',
        header: {
          'userToken': wx.getStorageSync('userToken'),
        },
        success(res) {
          if (res.data.code == 200) {
            wx.showToast({
              title: '操作成功',
              icon: 'success'
            })
            setTimeout(() => {
              wx.navigateBack()
            }, 2000)
          } else {
            wx.showToast({
              title: res.data.desc,
              icon: 'fail'
            })
            this.setData({
              disabled: false
            })
          }
        }
      })
    } else {
      wx.showToast({
        title: '请上传相关资料进行完善',
        icon: 'none'
      })
    }
  },
  getDetail() {
    let that = this
    wx.request({
      url: 'https://rongyaotupay.aokxm.com/api/nengliang/account/info',
      header: {
        'userToken': wx.getStorageSync('userToken'),
      },
      success(res) {
        if (res.data.code == 200) {
          let content = res.data.content
          if (!content) {
            return
          }
          that.setData({
            idNum: content.idcard,
            name: content.name,
            phone: content.phone,
            cardNo: content.bankcard,
            bankInfo: content.bankName,
            validDate: content.validDate,
            info: true
          })
        }
      }
    })
  },
})